package com.itcoon.cloud.framework.mybatis.metahandler;

import org.apache.ibatis.reflection.MetaObject;
import org.springframework.core.Ordered;

import java.time.LocalDateTime;

public class BaseMetaObjectHandler extends AbstractMetaObjectHandler {

    @Override
    public void insertFill(MetaObject metaObject) {
        LocalDateTime now = LocalDateTime.now();
        if(metaObject.hasGetter("createTime") && metaObject.getValue("createTime") == null){
            this.setFieldValByName("createTime", now, metaObject);
        }
        if(metaObject.hasGetter("updateTime") && metaObject.getValue("updateTime") == null){
            this.setFieldValByName("updateTime", now, metaObject);
        }
        if(metaObject.hasGetter("deleteFlag") && metaObject.getValue("deleteFlag") == null){
            this.setFieldValByName("deleteFlag", 0L, metaObject);
        }
    }

    public void updateFill(MetaObject metaObject) {
        this.setFieldValByName("updateTime", LocalDateTime.now(), metaObject);
    }



    @Override
    protected int getOrder() {
        return Ordered.HIGHEST_PRECEDENCE + 10;
    }
}
